package supercafe.more;

import supercafe.core.EditOrderDetailsScreen2;
import supercafe.core.MakeOrderDetailsScreen;
import supercafe.core.R;
import supercafe.core.ViewOrderScreen;
import supercafe.core.XMLHandler;
import supercafe.dataprovider.FinishPost;
import supercafe.defines.URLs;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import supercafe.dataprovider.DataPoster;

public class TasksScreen extends Activity implements Finish,FinishPost{
	private DataGetter datagetter;
	private DataPoster dataposter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        
        final Button bt_capnhat = (Button) findViewById(R.id.bt_capnhattacvu);
        final Button bt_ex = (Button) findViewById(R.id.bt_ex);
		datagetter = new DataGetter(this, this);
		
        bt_ex.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(TasksScreen.this, MainScreen.class);
				startActivity(i);
			}        	
        }); 
        bt_capnhat.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
		    	String link = URLs.TABLE_INFO+Profile.sessionId;
		    	datagetter.execute(link);
			}        	
        }); 
        Bundle b = this.getIntent().getExtras();
        final int areaPosition = b.getInt("areaPosition");
        final int tablePosition = b.getInt("tablePosition");
        //get table
        Table table = Area.areasWithTables.get(areaPosition).getTable(tablePosition);
        // get all menu button
        Button bt_datdonhangmoi = (Button)findViewById(R.id.bt_datdonhangmoi);
        bt_datdonhangmoi.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(TasksScreen.this, MakeOrderDetailsScreen.class);
				int tableId = Area.areasWithTables.get(areaPosition).getTable(tablePosition).getTableId();
				i.putExtra("tableId", tableId);
				startActivity(i);
			}
		});
        Button bt_chinhsuadonhang = (Button)findViewById(R.id.bt_chinhsuadonhang);
        bt_chinhsuadonhang.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(TasksScreen.this, EditOrderDetailsScreen2.class);
				int tableId = Area.areasWithTables.get(areaPosition).getTable(tablePosition).getTableId();
				i.putExtra("tableId", tableId);
				startActivity(i);
			}
		});
        Button bt_xemdonhang = (Button)findViewById(R.id.bt_xemdonhang);
        bt_xemdonhang.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(TasksScreen.this, ViewOrderScreen.class);
				int tableId = Area.areasWithTables.get(areaPosition).getTable(tablePosition).getTableId();
				i.putExtra("tableId", tableId);
				startActivity(i);
			}
		});
        Button bt_huydonhang = (Button)findViewById(R.id.bt_huydonhang);
        bt_huydonhang.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(TasksScreen.this);
				builder.setMessage("Bạn chắc chắn muốn hủy đơn hàng này?")
				       .setCancelable(false)
				       .setPositiveButton("Có", new DialogInterface.OnClickListener() {
				    	public void onClick(DialogInterface dialog, int id) {
				        	    dataposter = new DataPoster(TasksScreen.this,TasksScreen.this);
				        		int tableId = Area.areasWithTables.get(areaPosition).getTable(tablePosition).getTableId();
								String link = URLs.DELETE_ORDER + tableId;
								dataposter.execute(null,link,null);
				           }
				       })
				       .setNegativeButton("Không", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				                dialog.cancel();
				           }
				       });
				AlertDialog alert = builder.create();
				alert.show();
			}
		});
        // dat hang moi button
        if(!table.isUsed() || !table.isOrdered()){        	
        }
        else{
        	bt_datdonhangmoi.setEnabled(false);
        }
        // chinh sua don hang button, xem don hang
        if(table.isOrdered()){
        	// huy don hang
        	if(!table.isPrepared()){ 
        		
        		
        	}
        	else{
        		bt_huydonhang.setEnabled(false);
        	}
        }
        else{
        	bt_chinhsuadonhang.setEnabled(false);
        	bt_xemdonhang.setEnabled(false);
        	bt_huydonhang.setEnabled(false);
        }
        
        //Toast.makeText(this,b.getInt("tablePosition")+" "+b.getInt("areaPosition"), Toast.LENGTH_LONG).show();
        /*
        bt_xacnhan.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Intent i = new Intent(TasksScreen.this, TasksScreen.class);
				//startActivity(i);
			}
		});
        bt_ex.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(TasksScreen.this, Manhinhchinh.class);
				startActivity(i);
			}        	
        });  
        */  
    }
	public void doFinish() {
		// TODO Auto-generated method stub
		
	}   
	@Override
	public void doFinishPost() {
		// TODO Auto-generated method stub
		//posting order finished
		String resultXML = dataposter.getResponseResult();
		String requestResult = XMLHandler.getMessage(resultXML);
		if(requestResult==null){
			requestResult = "Đã hủy đơn hàng thành công";
			// chuyển qua activity MainScreen
			Toast.makeText(this, requestResult, Toast.LENGTH_LONG).show();
			Intent mainScreen = new Intent(this, MainScreen.class);
			startActivity(mainScreen);
			finish();
			return ;
		}
		Toast.makeText(this, requestResult, Toast.LENGTH_LONG).show();
	}	
}
